package com.xxq.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BaseDao {
	
	public static boolean updateSql(String sql ,String...strings ){
		Connection conn = null;
		PreparedStatement pstmt = null;
		conn = DBManager.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			if(strings!=null){
				for (int i = 0; i < strings.length; i++) {
					pstmt.setObject(i + 1, strings[i]);
				}
			}
			int i = pstmt.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBManager.closeConnection(conn);
		}
		return false;
	}
	public static List<Map<String, Object>> findList(String sql ,String...strings ){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		conn = DBManager.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			if(strings!=null){
				for (int i = 0; i < strings.length; i++) {
					pstmt.setObject(i + 1, strings[i]);
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				list.add(toEntity(rs));//���������
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBManager.closeConnection(conn);
		}
		return list;
	}
	public static Map<String, Object> findOne(String sql,String...strings) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		conn = DBManager.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			if(strings!=null){
				for (int i = 0; i < strings.length; i++) {
					pstmt.setObject(i + 1, strings[i]);
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				return toEntity(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBManager.closeConnection(conn);
		}
		return null;
	}
	public static String findOneString(String sql,String...strings) { //��ȡĳһ�е�ֵ
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		conn = DBManager.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			if(strings!=null){
				for (int i = 0; i < strings.length; i++) {
					pstmt.setObject(i + 1, strings[i]);
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				return rs.getString(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBManager.closeConnection(conn);
		}
		return null;
	}
	public static boolean delteDate(String sql,String...strings)  {
		Connection conn = null;
		PreparedStatement pstmt = null;
// rs = null;
		conn = DBManager.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			if(strings!=null){
				for (int i = 0; i < strings.length; i++) {
					pstmt.setObject(i + 1, strings[i]);
				}
			}
			boolean s=pstmt.execute(sql);
			return !s;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			DBManager.closeConnection(conn);
		}
		
	}
	
	public static Map<String, Object> toEntity(ResultSet rs) {
		Map<String, Object>map = new HashMap<String, Object>();
		try {
			ResultSetMetaData data = (ResultSetMetaData) rs.getMetaData();//��ȡ ����sql�Ĳ�ѯ�ֶ�
			for (int i = 1; i <= data.getColumnCount(); i++) {
				String columnName = data.getColumnName(i);
				map.put(columnName, rs.getObject(columnName));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	
	public static void main(String[] args) {
//		String sql2="update jiudian set jiudianname='"+"1"+"',jiudianlocation='"+"1"+"', jiudianroomtotal='"+100+"',jiudianroomprice='"+100
//				+"',jiudianshengyu='"+100+"' where jiudianname='"+"chennian1"+"'";
		
		String sql="select * from dingdan where username='"+"18099486696"+"'";
		System.out.println(updateSql(sql));
	}
}



/*
 * 
 //		String sql ="INSERT INTO `user` ( `loginname`, `loginpass`, `age`, `wealth`, `remark`, `sex`) VALUES"
//				+ " ( 'lisi0221', '123456', '50', '1000.000', 'ffd', '2');"; 
//		String sql1 ="INSERT INTO `user` ( `loginname`, `loginpass`, `age`, `wealth`, `remark`, `sex`) VALUES"
//				+ " ( ?, ?, ?, ?, ?, ?);"; 
//		//System.out.println("updateSql="+updateSql(sql));
//		//System.out.println("updateSql="+updateSql(sql1,"44ffff","123456","40","1000.000","ddd","2"));
//		String id = null;
//		
//		String findsql ="select * from user u join xxx x on x.userid = u.userid where 1=1 "; 
//		String condsql = "";
//		int stratPage = 0;
//		int endPage = 0;
//		int echePage = 5;
//		int page = 1;
//		stratPage = echePage * (page-1);
//		endPage = echePage * page;
//		if(!StringUtils.isNullOrEmpty(id)){
//			condsql = " and userid= '"+id+"'" ;
//		}
//		//DESC, ASC
//		//SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC limit 0,5
//		String ordersql = " order by u.userid DESC ";
//		String limitSql = " limit "+stratPage+","+endPage+"";
//		findsql = findsql + condsql+ordersql+limitSql;
//		
//		System.out.println(findsql);
//		@SuppressWarnings("rawtypes")
//		List list = findList(findsql, null);
//		for (int i = 0; i < list.size(); i++) {
//			System.out.println(list.get(i));
//		}
//		String sql="select * from user";
//		System.out.println(findList(sql));
//		List<Map<String, Object>> list=findList(sql);
//		for(int i=0;i<list.size();i++) {
//			System.out.println(list.get(i));
//		}
//		String sql1="select loginname,password,sex from user where userid="+"?";
//		System.err.println(findList(sql1, "3"));
//		list=findList(sql1);
//		for(int i=0;i<list.size();i++) {
//			System.out.println(list.get(i));
//		}
//		String sql="select * from user order by hhhhh ASC limit 1,2";
//		System.out.println(findOne(sql));
//		String sql = "select *from users;";
//		System.out.println(findList(sql));
//		System.out.println(findOneString(sql));
//		List<Map<String, Object>> list=findList(sql);
//		for(int i=0;i<list.size();i++) {
//			System.out.println(list.get(i));
//		}
		
 */
